package com.zego.wawaji.ui.center.pay.bill;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import com.zego.wawaji.R;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.http.request.HttpResponse;
import com.zego.wawaji.http.request.RequestComplete;
import com.zego.wawaji.http.request.UrlParam;
import com.zego.wawaji.ui.base.BasePanel;
import com.zego.wawaji.ui.base.CustomRecyclerView;
import com.zego.wawaji.ui.base.xlistview.ExListView;
import com.zego.wawaji.ui.center.pay.bean.Consume;
import com.zego.wawaji.ui.center.pay.bean.ConsumeList;

import java.util.ArrayList;
import java.util.List;

/**
 * 消费记录
 * Created by Su on 2017/11/27.
 */
public class ConsumeRecord extends BasePanel implements RequestComplete, ExListView.IXListViewListener {
    private CustomRecyclerView statusLayout;
    private ExListView exListView;
    private int page = 1; // 加载页码

    private ConsumeAdapter adapter;
    private List<Consume> list = new ArrayList<>();

    public ConsumeRecord(Context context) {
        super(context);
        setContentView(R.layout.base_recycler_view);
        initView();
        onRefresh();
    }

    private void initView() {
        statusLayout = (CustomRecyclerView) findViewById(R.id.statusLayout);
        View mViewTop = LayoutInflater.from(getContext()).inflate(R.layout.layout_margintop, null);
        exListView = statusLayout.getExListView();
        exListView.setXListViewListener(this);
        exListView.setPullRefreshEnable(true);
        exListView.setPullLoadEnable(true);
        exListView.addHeaderView(mViewTop);
        adapter = new ConsumeAdapter(getContext(), list);
        exListView.setAdapter(adapter);
        statusLayout.showLoading();
    }

    @Override
    public void onRefresh() {
        exListView.setPullRefreshEnable(true);
        exListView.setPullLoadEnable(true);
        page = 1;
        ModuleMgr.getWawaMgr().reqSpendRecord(page, this);
    }

    @Override
    public void onLoadMore() {
        page++;
        ModuleMgr.getWawaMgr().reqSpendRecord(page, this);
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        exListView.stopRefresh();
        exListView.stopLoadMore();
        if (response.getUrlParam() == UrlParam.reqSpendRecord) {
            setData(response);
        }
    }

    private void setData(HttpResponse response) {
        // 请求失败
        if (!response.isOk()) {
            if (list.size() != 0) {
                if (list.size() < 10) {
                    exListView.setPullLoadEnable(false);
                }
                adapter.notifyDataSetChanged();
                statusLayout.showExListView();
                return;
            }
            statusLayout.showNoData("暂无数据", "重试", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    statusLayout.showLoading();
                    onRefresh();
                }
            });
            return;
        }

        // 非缓存
        if (!response.isCache()) {
            ConsumeList dataList = (ConsumeList) response.getBaseData();

            if (dataList.getConsumeList().size() != 0) {
                //如果请求的第一页 应该把之前的数据都清掉，把返回的数据作为第一页
                if (page == 1) {
                    if (list.size() != 0) {
                        list.clear();
                    }
                    page = 1;
                }

                list.addAll(dataList.getConsumeList());
                if (list.size() < 10) {
                    exListView.setPullLoadEnable(false);
                }
                adapter.notifyDataSetChanged();
                statusLayout.showExListView();
            } else {
                if (page == 1) {
                    statusLayout.showNoData("暂无数据", "重试", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            statusLayout.showLoading();
                            onRefresh();
                        }
                    });
                } else {
                    exListView.setPullLoadEnable(false);
                }
            }
            return;
        }

        // 缓存
        if (page == 1) {
            ConsumeList dataList = (ConsumeList) response.getBaseData();

            if (dataList != null && dataList.getConsumeList().size() != 0) {
                if (list.size() != 0) {
                    list.clear();
                }
                list.addAll(dataList.getConsumeList());
                if (list.size() < 10) {
                    exListView.setPullLoadEnable(false);
                }
                adapter.notifyDataSetChanged();
                statusLayout.showExListView();
            }
        }
    }

    /**
     * 刷新首页
     */
    public void refreshList() {
        onRefresh();
        if (adapter != null && adapter.getList() != null && adapter.getList().size() != 0) {
            exListView.setSelection(0);
        }
    }
}
